package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.EmployeeArchiveDao;
import com.zhangtai.modules.entity.employee.EmployeeArchiveEntity;
import com.zhangtai.modules.service.employee.EmployeeArchiveService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
public class EmployeeArchiveServiceImpl extends ServiceImpl<EmployeeArchiveDao, EmployeeArchiveEntity> implements EmployeeArchiveService {

    @Override
    public PageResult queryPage(@RequestParam(value = "employeeOid", required = false) String employeeOid,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "orgname", required = false) List<Long> orgname,
                                @RequestParam(value = "unitname", required = false) List<Long> unitname,
                                @RequestParam(value = "jobname", required = false) List<Long> jobname,
                                @RequestParam(value = "status",required = false)List<Integer> status,
                                @RequestParam(value = "isReinstate", required = false) Integer isReinstate,
                                @RequestParam(value = "isEntryStatus", required = false) Integer isEntryStatus,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            ,@RequestParam(value = "orgIds") List<Long> orgIds,
                                @RequestParam(value = "socialTrainee", required = false) Integer socialTrainee
            ,@RequestParam(value="hire_date",required = false) String hireDate,
                                @RequestParam(value="isOffer",required = false)      Integer isOffer,
                                @RequestParam(value="code",required = false) String code
    ) {

        Integer i=0;
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(0);
        integers.add(1);
       /* IPage<EmployeeArchiveEntity> page = this.page(
                new Page<EmployeeArchiveEntity>(pageNum,pageSize),
                new QueryWrapper<EmployeeArchiveEntity>()
                        .like(!StringUtils.isEmpty(name),"name", name).in(!CollectionUtils.isEmpty(orgname),"org_id", orgname)
                        .eq(orgname!=null&&orgname.size()==0,"org_id", null).eq(unitname!=null&&unitname.size()==0,"unit_id", unitname)
                        .eq(jobname!=null&&jobname.size()==0,"job_id", null)
                        .in(!CollectionUtils.isEmpty(unitname),"unit_id", unitname).in(!CollectionUtils.isEmpty(jobname),"job_id", jobname)
                        .eq("is_delete",0).eq(isReinstate!=null,"is_reinstate",isReinstate).eq(isEntryStatus!=null,"is_entry_status",isEntryStatus)
                        .eq(!StringUtils.isEmpty(employeeOid),"employee_oid",employeeOid)
                        .eq(!ObjectUtils.isEmpty(socialTrainee),"social_trainee",socialTrainee)
                        .in(!CollectionUtils.isEmpty(status),"status",status)
                        .in(isEntryStatus!=null,"is_reinstate",integers)
                        .in(!CollectionUtils.isEmpty(orgIds),"org_id",orgIds)
                        .eq(CollectionUtils.isEmpty(orgIds),"org_id",null)
                        .le(hireDate!=null&&i.equals(isEntryStatus),"hiredate",hireDate)
                        .eq(isOffer!=null,"is_offer",isOffer)
                        .eq(!StringUtils.isEmpty(code),"principal_user_code",code)
                        .orderByDesc("update_time")
        );*/

        QueryWrapper<EmployeeArchiveEntity> employeeWrapper =  new QueryWrapper<EmployeeArchiveEntity>()
                .like(!StringUtils.isEmpty(name),"name", name).in(!CollectionUtils.isEmpty(orgname),"org_id", orgname)
                .eq(orgname!=null&&orgname.size()==0,"org_id", null).eq(unitname!=null&&unitname.size()==0,"unit_id", unitname)
                .eq(jobname!=null&&jobname.size()==0,"job_id", null)
                .in(!CollectionUtils.isEmpty(unitname),"unit_id", unitname).in(!CollectionUtils.isEmpty(jobname),"job_id", jobname)
                .eq("is_delete",0).eq(isReinstate!=null,"is_reinstate",isReinstate).eq(isEntryStatus!=null,"is_entry_status",isEntryStatus)
                .eq(!StringUtils.isEmpty(employeeOid),"employee_oid",employeeOid)
                .eq(!ObjectUtils.isEmpty(socialTrainee),"social_trainee",socialTrainee)
                .in(!CollectionUtils.isEmpty(status),"status",status)
                .in(isEntryStatus!=null,"is_reinstate",integers)
                .eq(CollectionUtils.isEmpty(orgIds),"org_id",null)
                .le(hireDate!=null&&i.equals(isEntryStatus),"hiredate",hireDate)
                .eq(isOffer!=null,"is_offer",isOffer);
        employeeWrapper.and(wra->wra.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).or().
                eq("principal_user_code",code).or().eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)) .orderByDesc("update_time");
        IPage<EmployeeArchiveEntity> page = this.page(
                new Page<EmployeeArchiveEntity>(pageNum,pageSize),employeeWrapper);

        return new PageResult(page);
    }

    @Override
    public EmployeeArchiveEntity getByIdE(Long id) {
            EmployeeArchiveEntity one = this.getOne(new QueryWrapper<EmployeeArchiveEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployeeArchiveEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(EmployeeArchiveEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            EmployeeArchiveEntity entity = new EmployeeArchiveEntity();
        entity.setOid(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}